package com.theten52.dev.code.fragment.dubbo.provider;

import org.I0Itec.zkclient.ZkClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

public class GetAllProviders {
    public List<String> getProviderIPList(String pkg) throws IOException {
        List<String> result = new ArrayList<>();
        String propertyPath = this.getClass().getResource("/").getPath() + File.separator + "application.properties";
        Properties pro = new Properties();
        FileInputStream in = new FileInputStream(propertyPath);
        pro.load(in);
        in.close();
        String zkAddress = pro.getProperty("dubbo.zookeeperAddress");
        String[] split = zkAddress.split("//");
        StringBuffer stringBuffer = new StringBuffer();
        if (split != null) {
            stringBuffer.append(split[split.length - 1]);
        } else {
            stringBuffer.append("zk01.local:2181");
        }
        ZkClient zkClient = new ZkClient(stringBuffer.toString(), 5000);
        List<String> list = zkClient.getChildren("/dubbo/" + pkg + "/providers");
        Iterator<String> it = list.iterator();
        List<String> providers = new ArrayList<>();
        while (it.hasNext()) {
            String str = it.next();
            providers.add(str);
        }

        return providers;
    }
}
